package com.akk.utils.interceptor;

import com.akk.module.avs.entity.AvsPayVipConfig;
import com.akk.module.avs.service.AvsPayVipConfigService;
import com.akk.utils.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Component
public class PayConfigInterceptor implements HandlerInterceptor {

    @Autowired
    private AvsPayVipConfigService configService;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
                List<AvsPayVipConfig> configs = configService.selectAll();
                String userId = httpServletRequest.getParameter("payerId");
                String userIds = "be1660ffe8724deeb113db7f3cf43e62";
                if (userId !=null && userIds.contains(userId)){
                    httpServletRequest.getSession().setAttribute("debug","debug");
                }
                // debug
                String debug = httpServletRequest.getSession().getAttribute("debug") == null ? "" : httpServletRequest.getSession().getAttribute("debug").toString();
                if (configs.size() != 0){
                    AvsPayVipConfig config = configs.get(0);
                    if (config.getIsOpen() == 1 || (debug != null && debug.equals("debug"))){
                        return true;
                    }
                    throw new BusinessException("暂未开放");
                }
                return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
